package com.develop.web.in;


import com.develop.data.loan.model.LoanContract;
import com.develop.data.loan.model.LoanInstallment;
import com.develop.web.in.service.LoanRepaymentPlanHubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class LoanRepaymentPlanController {

    @Autowired
    LoanRepaymentPlanHubService loanRepaymentPlanHubService;


    //需要配置json序列化、反序列化，从json类型的body转换成controller入参数
    //枚举类默认支持字面量和对象的转化
    @PostMapping(path = "/v1/repaymentPlan/preview", consumes = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseStatus(HttpStatus.CREATED)
    List<List<LoanInstallment>> preview(@RequestBody List<LoanContract> items) {


        return loanRepaymentPlanHubService.batchPreview(items);

        //return items.stream().map(item -> {
        //
        //    String productionNumber = item.getLoanProductionNumber();
        //    String businessNumber = item.getLoanBusinessNumber();
        //    LoanState loanState = item.getLoanState();
        //
        //    String serviceName = MessageFormat.format("com.develop.web.in.service.{0}{1}LoanInstallmentService", productionNumber, businessNumber);
        //    LoanRepaymentPlanPreviewService<LoanContract, LoanInstallment> service;
        //    try {
        //        service = (LoanRepaymentPlanPreviewService<LoanContract, LoanInstallment>) AppContext.getBean(Class.forName(serviceName));
        //    } catch (ClassNotFoundException e) {
        //        throw new RuntimeException(e);
        //    }
        //    return service.preview(item);
        //
        //}).collect(Collectors.toList());
    }

}
